class Solution:
    def fractionToDecimal(self, numerator: int, denominator: int) -> str:
        if denominator == 0 or numerator == 0:
            return '0'
        flag = True# 正负
        if numerator<0 and denominator>0 or numerator>0 and denominator<0:
            flag = False
            numerator, denominator = abs(numerator), abs(denominator)
        merchant = str(numerator // denominator)
        rest = numerator % denominator
        if rest == 0:
            return merchant if flag else '-' + rest
        merchant += '.'
        d = dict()
        while rest != 0:
            if d.get(rest):  # 出现两次
                position = d.get(merchant)
                merchant = merchant[:position] + '(' + merchant[position:] + ')'
                break
            else:
                d[rest] = len(merchant)
            rest *= 10
            merchant += str(merchant // denominator)
            rest %= denominator
        return merchant if flag else '-' + merchant

